<template>
  <el-form ref="formRef" :model="formModel" :rules="rules" label-width="130px" class="form" label-position="right" status-icon>
    <el-form-item label="所属城市:" prop="cityCode">
      <query-dict
        :dict-list="cityList"
        class="form-select"
        :disabled="initCityCode !== '00'"
        placeholder="请选择所属城市"
        :value.sync="formModel.cityCode"
      ></query-dict>
    </el-form-item>
    <el-form-item label="车辆号码:" prop="carNumber">
      <el-input v-model.trim="formModel.carNumber" :disabled="isEdit" class="form-item" placeholder="请输入车牌号码" clearable />
    </el-form-item>
    <el-form-item label="车辆型号:" prop="modelId">
      <query-dict :dict-list="typeList" filterable class="form-select" placeholder="请选择车辆车型" :value.sync="formModel.modelId"></query-dict>
    </el-form-item>
    <el-form-item label="车辆类型:" prop="category">
      <query-dict :dict-list="carTypeList" filterable class="form-select" placeholder="请选择车辆类型" :value.sync="formModel.category"></query-dict>
    </el-form-item>
    <el-form-item label="车辆颜色:" prop="color">
      <el-input v-model.trim="formModel.color" class="form-item" placeholder="请输入车辆颜色" clearable />
    </el-form-item>
    <el-form-item label="座位数:" prop="seats">
      <el-input v-model.trim="formModel.seats" class="form-item" placeholder="请输入座位数" clearable />
    </el-form-item>
    <el-form-item label="最大里程(KM):" prop="mileage">
      <el-input v-model.trim="formModel.mileage" class="form-item" placeholder="请输入最大里程" clearable />
    </el-form-item>
    <el-form-item label="最大速度(KM/H):" prop="speed">
      <el-input v-model.trim="formModel.speed" class="form-item" placeholder="请输入最大速度" clearable />
    </el-form-item>
    <el-form-item label="SIM卡号:" prop="phone">
      <el-input v-model.trim="formModel.phone" class="form-item" placeholder="请输入SIM卡号" clearable />
    </el-form-item>
    <el-form-item label="设备编号:" prop="cid">
      <el-input v-model.trim="formModel.cid" class="form-item" placeholder="请输入设备编号" clearable />
    </el-form-item>
    <el-form-item label="发动机号:" prop="engineno">
      <el-input v-model.trim="formModel.engineno" class="form-item" placeholder="请输入发动机号" clearable />
    </el-form-item>
    <el-form-item label="车架号:" prop="classno">
      <el-input v-model.trim="formModel.classno" class="form-item" placeholder="请输入车架号" clearable />
    </el-form-item>
    <el-form-item label="车辆年检时间:" prop="annualVehicleInspectionTime">
      <el-date-picker
        class="select-item"
        v-model="formModel.annualVehicleInspectionTime"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择车辆年检时间"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="车辆所属:" prop="owner">
      <query-dict :dict-list="carOwnerList" filterable class="form-select" placeholder="请选择车辆所属" :value.sync="formModel.owner"></query-dict>
    </el-form-item>
    <!-- <el-form-item label="车辆小图片:" prop="smallCarImgurl">
      <el-input v-model.trim="formModel.smallCarImgurl" class="form-item" placeholder="请输入车辆小图片" clearable />
    </el-form-item> -->
    <!-- <el-form-item label="车辆小图片:">
      <c-upload
        ref="smallCarImgurl"
        class="pic"
        auto-upload
        action="/api/upload/file"
        list-type="picture-card"
        :http-request="uploadHandle"
        :size="10"
        :limit="1"
        :fileList="formModel.smallCarImgurl"
        @on-success="uploadSuccess"
        :on-remove="uploadRemove"
        :on-preview="uploadReview"
        @before-upload="uploadBefore('smallCarImgurl')"
      >
        <i class="el-icon-plus"></i>
        <div class="info">只能上传png,jpg,jpeg,gif图片文件，且不能大于10M</div>
      </c-upload>
    </el-form-item> -->
    <!-- <el-form-item label="车辆图片:" prop="carImguri">
      <el-input v-model.trim="formModel.carImguri" class="form-item" placeholder="请输入车辆图片" clearable />
    </el-form-item> -->
  </el-form>
</template>

<script>
// import CUpload from 'components/upload'
import dictObj from '@/store/dictData'
import MixinForm from 'mixins/form'
import utils from 'utils'

export default {
  mixins: [MixinForm],
  components: {
    // CUpload
  },
  props: {
    initData: {
      type: Object,
      default() {
        return {}
      }
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      cityList: [], // 业务线集合
      typeList: [], // 车辆车型
      carTypeList: dictObj.carTypeList,
      carOwnerList: dictObj.carOwnerList,
      initCityCode: '',
      rules: {
        cityCode: [{ required: true, message: '请选择所属城市', trigger: 'change' }],
        carNumber: [{ required: true, message: '请输入车牌号码', trigger: 'blur' }],
        modelId: [{ required: true, message: '请选择车辆车型', trigger: 'change' }],
        cid: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
        category: [{ required: true, message: '请选择车辆类型', trigger: 'change' }],
        engineno: [{ required: true, message: '请输入发动机号', trigger: 'blur' }],
        classno: [{ required: true, message: '请输入车架号', trigger: 'blur' }],
        speed: [{ required: true, message: '请输入最大速度', trigger: 'blur' }],
        seats: [{ required: true, message: '请输入座位数', trigger: 'blur' }],
        mileage: [{ required: true, message: '请输入最大里程', trigger: 'blur' }],
        annualVehicleInspectionTime: [{ required: true, message: '请选择车辆年检时间', trigger: 'change' }],
        owner: [{ required: true, message: '请选择车辆所属', trigger: 'change' }],
        phone: [{ required: true, message: '请输入SIM卡号', trigger: 'blur' }]
      }
    }
  },
  computed: {
    formModel: {
      get() {
        return this.initData
      },
      set(val) {
        this.$emit('update:init-data', val)
      }
    }
  },
  created() {
    const { cityCode, cityName } = utils.getStore('cityInfo')
    const cityList = utils.getStore('cityList')
    if (cityCode === '00') {
      this.initCityCode = '00'
      this.cityList = this.$store.getters.allCity.length ? this.$store.getters.allCity : cityList
    } else {
      this.cityList = [
        {
          label: cityName,
          value: cityCode
        }
      ]
      this.formModel.cityCode = cityCode
      this.initCityCode = '11'
    }
    this.getTypeList()
  },
  methods: {
    getTypeList() {
      this.$api.vehicle
        .fetch('carTypeList', {
          rows: 1,
          offset: 100
        })
        .then(res => {
          const typeList = res.data.map(val => ({
            label: val.modelId,
            value: val.id
          }))
          this.typeList = typeList || []
        })
    },
    uploadHandle(file) {
      this.$refs[this.uploadType].customUpload(file)
    },
    uploadSuccess(response, file, fileList) {
      this.formModel[this.uploadType] = [fileList[fileList.length - 1]]
    },
    uploadRemove(file, fileList) {
      this.formModel[file.ref] = fileList
    },
    uploadReview(file) {
      this.dialogObj = {
        ...this.dialogObj,
        imageUrl: file.url,
        isShow: true
      }
    },
    uploadBefore(ref) {
      this.uploadType = ref
    }
  }
}
</script>

<style lang="less" scoped>
.form {
  margin-right: 50px;
  .form-item {
    width: 100%;
  }
  .form-select {
    width: 50%;
  }
  .pic {
    padding-bottom: 25px;
    span {
      display: inline-block;
      margin-bottom: 10px;
    }
    .info {
      height: 20px;
      margin-top: 5px;
      font-size: @f12;
      line-height: 20px;
    }
  }
}
</style>
